using System;
using NUnit.Framework;

namespace BeginningCSharp.Tests
{
   [TestFixture]
   public class LCMSpike
   {
      [Test]
      public void LCM_test()
      {
         Assert.AreEqual(15, LCM(3, 5));
         Assert.AreEqual(15, LCM(5, 3));
         Assert.AreEqual(12, LCM(4, 6));
         Assert.AreEqual(12, LCM(6, 4));
         Assert.AreEqual(3, LCM(3, 3));
         Assert.AreEqual(1, LCM(1, 1));
         Assert.AreEqual(0, LCM(0, 0));
      }

      // see http://en.wikipedia.org/wiki/Least_common_multiple#Reduction_by_the_greatest_common_divisor
      private int LCM(int a, int b)
      {
         return a == 0 || b == 0 ? 0 : a * (b / GCD(a, b));
      }

      // see http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
      private int GCD(int a, int b)
      {
         return b == 0 ? a : GCD(b, a % b);
      }
   }
}